import requests
from urllib.parse import urlparse, parse_qs
from encryption_module import getDES3Token, generate_time_code
from request_module import send_userAuthNew_request


user_name = "19309481507"
# 填手机号

# 获取本机IP
response = requests.get('http://100.95.0.1', allow_redirects=False)
#print(response.text)

try:
        # 使用字符串操作提取 <NextURL> 标签中的内容
    next_url = response.text.split('<NextURL>')[1].split('</NextURL>')[0]

        # 解析 URL 查询参数
    parsed_url = urlparse(next_url)
    query_params = parse_qs(parsed_url.query)

        # 提取 wlanuserip 和 wlanacname
    wlanuserip = query_params.get('wlanuserip', [''])[0]
    wlanacname = query_params.get('wlanacname', [''])[0]
#    wlanuserip = "100.95.137.169"
    
    print(f'wlanuserip: {wlanuserip}')
#    print(f'wlanacname: {wlanacname}')
        
except IndexError:
    print("无法找到IP")
    print(response.text)

# 加密参数
time_code = generate_time_code()
print(time_code)

encrypted_user_ip= getDES3Token(wlanuserip,time_code)
#print(encrypted_user_ip)
encrypted_user_name = getDES3Token(user_name,time_code)
#print(encrypted_user_name)
encrypted_wlanacname = getDES3Token(wlanacname,time_code)
#print(encrypted_wlanacname)

encrypted_password = "0aVtgQrG/a2UUiq5ETM1YQ=="

try:
    response_content = send_userAuthNew_request(wlanuserip,wlanacname,encrypted_user_ip,encrypted_user_name,encrypted_wlanacname,encrypted_password,time_code)
    print("Response Text:", response_content)
except Exception as e:
    print("Error occurred:", str(e))